import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ListLinks {

    public static List<Scu> scuList = new ArrayList<Scu>();

    public static void main(String[] args) throws IOException {

        Integer i = 0;

        System.out.println("Starting.............");



        String mens = "http://www.carlopazolini.com/ru/collection/men/shoes?all=1";
        String womens = "http://www.carlopazolini.com/ru/collection/women/shoes?all=1";

        Document docMen = Jsoup.connect(mens).get();
        Document docWomen = Jsoup.connect(womens).get();

        Elements linksMen = docMen.select("a[href].content");
        Elements linksWomen = docWomen.select("a[href].content");



        System.out.println("print started at: " + new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()));

        for (Element link : linksMen) {
             printPrices(link.attr("abs:href"), "men");

        }
        for (Element link : linksWomen) {
            printPrices(link.attr("abs:href"), "women");

        }

        System.out.println("export started at: " + new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()));
        exportCsvFile("c:\\temp/carlo.csv", scuList);
        System.out.println("export finished at: " + new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()));




    }
    private static void print(String msg, Object... args) {
        System.out.println(String.format(msg, args));
    }
    private static String trim(String s, int width) {
        if (s.length() > width)
            return s.substring(0, width-1) + ".";
        else
            return s;
    }

    private static void printPrices(String scu, String type) throws IOException {

        Document docSCU = Jsoup.connect(scu).get();
        String category = "", item = "", price = "";

        category =  docSCU.select("h1").first().text();
        item = docSCU.select("h3").first().text();
        price = docSCU.select("p.price.size25").first().text();

        Scu newSCU = new Scu(category, item, price, type);
        System.out.println("Add new  SCU: " + newSCU.getScu() + ", " + newSCU.getType());
        scuList.add(newSCU);

    }


    private static void exportCsvFile(String sFileName, List<Scu> scus)
    {

        try
        {
            FileWriter writer = new FileWriter(sFileName);

            writer.append("Category");
            writer.append(',');
            writer.append("scu");
            writer.append(',');
            writer.append("price");
            writer.append(',');
            writer.append("type");
            writer.append('\n');

            for(Scu item : scus) {
                writer.append(item.getCategory());
                writer.append(',');
                writer.append(item.getScu());
                writer.append(',');
                writer.append(item.getPrice());
                writer.append(',');
                writer.append(item.getType());
                writer.append('\n');
            }

            //generate whatever data you want

            writer.flush();
            writer.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

    }

}
